home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / readwrite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.2 KB  |  343 lines

  1. /*
  2. ** Copyright 1994, Silicon Graphics, Inc.
  3. ** All Rights Reserved.
  4. **
  5. ** This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6. ** the contents of this file may not be disclosed to third parties, copied or
  7. ** duplicated in any form, in whole or in part, without the prior written
  8. ** permission of Silicon Graphics, Inc.
  9. **
  10. ** RESTRICTED RIGHTS LEGEND:
  11. ** Use, duplication or disclosure by the Government is subject to restrictions
  12. ** as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13. ** and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14. ** successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15. ** rights reserved under the Copyright Laws of the United States.
  16. **
  17. */
  18. #include <GL/glx.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <X11/keysym.h>
  24.  
  25. static int RGB_SB_attributes[] = {
  26.     GLX_RGBA,
  27.     GLX_RED_SIZE, 1,
  28.     GLX_GREEN_SIZE, 1,
  29.     GLX_BLUE_SIZE, 1,
  30.     GLX_DEPTH_SIZE, 1,
  31.     GLX_STENCIL_SIZE, 1,
  32.     None,
  33. };
  34.  
  35. static int CI_SB_attributes[] = {
  36.     GLX_DEPTH_SIZE, 1,
  37.     GLX_STENCIL_SIZE, 1,
  38.     None,
  39. };
  40.  
  41. static int RGB_DB_attributes[] = {
  42.     GLX_RGBA,
  43.     GLX_RED_SIZE, 1,
  44.     GLX_GREEN_SIZE, 1,
  45.     GLX_BLUE_SIZE, 1,
  46.     GLX_DOUBLEBUFFER,
  47.     GLX_DEPTH_SIZE, 1,
  48.     GLX_STENCIL_SIZE, 1,
  49.     None,
  50. };
  51.  
  52. static int CI_DB_attributes[] = {
  53.     GLX_DOUBLEBUFFER,
  54.     GLX_DEPTH_SIZE, 1,
  55.     GLX_STENCIL_SIZE, 1,
  56.     None,
  57. };
  58.  
  59.  
  60. Display *dpy;
  61. Window window;
  62. int rgb = 1;
  63. GLenum direct = GL_TRUE;
  64. int W = 600;
  65. int H = 600;
  66. int doubleBuf = 1;
  67. int dither = 1;
  68. char rawData[1280*1024*4];    /* big enough for a cheap demo */
  69.  
  70. static void Init(void)
  71. {
  72.     if (rgb) {
  73.     glClearColor(0,0,0,0);
  74.     } else {
  75.     glClearIndex(0);
  76.     }
  77.  
  78.     glClear(GL_COLOR_BUFFER_BIT);
  79.     glReadBuffer(GL_FRONT);
  80.     glDrawBuffer(GL_FRONT);
  81. }
  82.  
  83. static void Redraw(void)
  84. {
  85.     int i;
  86.     int r,g;
  87.  
  88.     glViewport(0,0,W,H);
  89.     glMatrixMode(GL_PROJECTION);
  90.     glLoadIdentity();
  91.     glOrtho(0,W,0,H,-1,1);
  92.     glMatrixMode(GL_MODELVIEW);
  93.  
  94.     glClear(GL_COLOR_BUFFER_BIT);
  95.  
  96.     r = g = 0;
  97.  
  98.     glIndexf(0);
  99.     glBegin(GL_QUAD_STRIP);
  100.     glColor3ub(r,g,0);
  101.     glVertex2f(0, 0);
  102.     glColor3ub(r,g,255);
  103.     glVertex2f(0, H);
  104.  
  105.     r = 255;
  106.  
  107.     glIndexf(85);
  108.     glColor3ub(r,g,0);
  109.     glVertex2f(W/3, 0);
  110.     glColor3ub(r,g,255);
  111.     glVertex2f(W/3, H);
  112.  
  113.     g = 255;
  114.  
  115.     glIndexf(170);
  116.     glColor3ub(r,g,0);
  117.     glVertex2f(W*2/3, 0);
  118.     glColor3ub(r,g,255);
  119.     glVertex2f(W*2/3, H);
  120.  
  121.     r = 0;
  122.  
  123.     glIndexf(255);
  124.     glColor3ub(r,g,0);
  125.     glVertex2f(W, 0);
  126.     glColor3ub(r,g,255);
  127.     glVertex2f(W, H);
  128.  
  129.     glEnd();
  130. }
  131.  
  132. static void pixelsTest(void)
  133. {
  134.     if (rgb) {
  135.     glReadPixels(0,0,W,H,GL_RGBA,GL_UNSIGNED_BYTE,rawData);
  136.     glRasterPos2f(0,0);
  137.     glDrawPixels(W,H,GL_RGBA,GL_UNSIGNED_BYTE,rawData);
  138.     } else {
  139.     glReadPixels(0,0,W,H,GL_COLOR_INDEX,GL_UNSIGNED_BYTE,rawData);
  140.     glRasterPos2f(0,0);
  141.     glDrawPixels(W,H,GL_COLOR_INDEX,GL_UNSIGNED_BYTE,rawData);
  142.     }
  143. }
  144.  
  145. static void blendTest(void)
  146. {
  147.     if (!rgb) {
  148.     printf("blend Test not supported in color index mode.\n");
  149.     return;
  150.     }
  151.     glEnable(GL_BLEND);
  152.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  153.     glColor4f(1.0, 1.0, 1.0, 0.0);
  154.     glBegin(GL_QUADS);
  155.     glVertex2f(0,0);
  156.     glVertex2f(W,0);
  157.     glVertex2f(W,H);
  158.     glVertex2f(0,H);
  159.     glEnd();
  160.     glDisable(GL_BLEND);
  161.     glFlush();
  162. }
  163.  
  164. static void Usage(void)
  165. {
  166.     fprintf(stderr, "Usage: program [-c] [-s] [-i]\n");
  167.     fprintf(stderr, " -c  run in color index mode\n");
  168.     fprintf(stderr, " -s  run in singlebuffer mode\n");
  169.     fprintf(stderr, " -i  turn direct rendering off--go thru the X server\n");
  170.     exit(1);
  171. }
  172.  
  173. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  174. {
  175.     if ((e->type == MapNotify) && (e->xmap.window == (Window) arg)) {
  176.     return GL_TRUE;
  177.     }
  178.     return GL_FALSE;
  179. }
  180.  
  181. int main(int argc, char** argv)
  182. {
  183.     XVisualInfo *vi;
  184.     Colormap cmap;
  185.     XSetWindowAttributes swa;
  186.     GLXContext cx;
  187.     XEvent event;
  188.     GLboolean needDisplay;
  189.     XColor white;
  190.     int i;
  191.  
  192.     rgb = 1;
  193.     direct = GL_TRUE;
  194.     for (i = 1; i < argc; i++) {
  195.         if (argv[i][0] == '-') {
  196.             switch (argv[i][1]) {
  197.               case 'c':
  198.                 rgb = GL_FALSE;
  199.                 break;
  200.           case 's':
  201.         doubleBuf = 0;
  202.         break;
  203.           case 'i':
  204.         direct = GL_FALSE;
  205.         break;
  206.               default:
  207.                 Usage();
  208.             }
  209.         } else {
  210.             Usage();
  211.         }
  212.     }
  213.  
  214.     dpy = XOpenDisplay(0);
  215.     if (!dpy) {
  216.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  217.     return -1;
  218.     }
  219.  
  220.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  221.         doubleBuf ? (rgb ? RGB_DB_attributes : CI_DB_attributes) :
  222.         (rgb ? RGB_SB_attributes : CI_SB_attributes));
  223.     if (!vi) {
  224.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  225.         getenv("DISPLAY"));
  226.     return -1;
  227.     }
  228.  
  229.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  230.                rgb ? AllocNone : AllocAll);
  231.     if (rgb) {
  232.     white.red = ~0;
  233.     white.green = ~0;
  234.     white.blue = ~0;
  235.     XAllocColor(dpy, cmap, &white);
  236.     swa.background_pixel = white.pixel;
  237.     } else {
  238.     swa.background_pixel = 15;
  239.     }
  240.  
  241.     if (!rgb) {
  242.     XColor buf;
  243.     int i;
  244.  
  245.     buf.flags = DoRed | DoGreen | DoBlue;
  246.  
  247.     /* Init color map */
  248.     for (i=0; i<16; i++) {
  249.         buf.pixel = i;
  250.         buf.blue = (i & 4) ? 65535 : 0;
  251.         buf.green = (i & 2) ? 65535 : 0;
  252.         buf.red = (i & 1) ? 65535 : 0;
  253.         if (i > 8) {
  254.         buf.red /= 2;
  255.         buf.green /= 2;
  256.         buf.blue /= 2;
  257.         }
  258.         XStoreColor(dpy, cmap, &buf);
  259.     }
  260.     }
  261.  
  262.     swa.border_pixel = 0;
  263.     swa.background_pixel = white.pixel;
  264.     swa.colormap = cmap;
  265.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  266.     | KeyReleaseMask;
  267.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  268.                W, H,
  269.                0, vi->depth, InputOutput, vi->visual,
  270.                CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
  271.                &swa);
  272.     XSetWMColormapWindows(dpy, window, &window, 1);
  273.     XMapWindow(dpy, window);
  274.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  275.  
  276.     cx = glXCreateContext(dpy, vi, 0, direct);
  277.     if (!glXMakeCurrent(dpy, window, cx)) {
  278.     fprintf(stderr, "Can't make window current to context\n");
  279.     return -1;
  280.     }
  281.  
  282.     Init();
  283.  
  284.     needDisplay = GL_TRUE;
  285.     for (;;) {
  286.     do {
  287.         XNextEvent(dpy, &event);
  288.         switch (event.type) {
  289.           case Expose:
  290.         needDisplay = GL_TRUE;
  291.         break;
  292.           case ConfigureNotify:
  293.         W = event.xconfigure.width;
  294.         H = event.xconfigure.height;
  295.         needDisplay = GL_TRUE;
  296.         break;
  297.           case KeyPress:
  298.         {
  299.             char buf[100];
  300.             int rv;
  301.             KeySym ks;
  302.  
  303.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  304.             switch (ks) {
  305.               case XK_P: case XK_p:
  306.             printf("pixels test (ReadPixels followed by DrawPixels)\n");
  307.             pixelsTest();
  308.             break;
  309.               case XK_B: case XK_b:
  310.             printf("blend test\n");
  311.             blendTest();
  312.             break;
  313.               case XK_R: case XK_r:
  314.             needDisplay = GL_TRUE;
  315.             break;
  316.               case XK_D: case XK_d:
  317.             dither = 1-dither;
  318.             if (dither) {
  319.                 glEnable(GL_DITHER);
  320.                 printf("Dithering on\n");
  321.             } else {
  322.                 glDisable(GL_DITHER);
  323.                 printf("Dithering off\n");
  324.             }
  325.             needDisplay = GL_TRUE;
  326.             break;
  327.               case XK_Escape:
  328.             return 0;
  329.               default:
  330.             break;
  331.             }
  332.         }
  333.         break;
  334.         }
  335.     } while (XPending(dpy) != 0);
  336.  
  337.     if (needDisplay) {
  338.         needDisplay = GL_FALSE;
  339.         Redraw();
  340.     }
  341.     }
  342. }
  343.